import { REDIS_KEY } from '../declare/keys';
import { LiverBaseInfo } from '../declare/liver';
import { getRedisClient } from '../library/redis';
import { findOne } from '../service/mongoUtil';

const redis = getRedisClient()

export async function getRankDetail(date?: string) {

  const redisKey = date? `${date}:${REDIS_KEY.REDIS_KEY_GIFT_RANK}`:REDIS_KEY.REDIS_KEY_GIFT_RANK
  const dataSet = await redis.zrevrange(redisKey, 0, -1, 'WITHSCORES')
  //console.log(dataSet)
  const dataWater: { authorId: number, name: string, rank: number, value: string }[] = []
  let rank = 1;
  for (let index = 0; index < dataSet.length; index += 2) {
    const authorId = Number(dataSet[index])
    const score = Number(dataSet[index + 1])
    const liverInfo: LiverBaseInfo = await findOne("t_liver_base_info", { id: authorId })
    dataWater.push({ authorId, value: (score/10000).toFixed(1), rank, name: liverInfo.name })
    rank += 1
  }
  //console.log(dataWater)
  return dataWater
};

//saveWaterData(new Date())